package com.sntra.beibi.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.JSONObject;
import com.sntra.beibi.constant.Constants;
import com.sntra.beibi.domain.Book;
import com.sntra.beibi.service.impl.BookServiceImpl;


@RestController
@Controller
public class BookController {

    @Autowired
    private BookServiceImpl bookService;
    


    @Configuration
    public class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            String os = System.getProperty("os.name");
            if (os.toLowerCase().startsWith("win")) { // windos系统
                registry.addResourceHandler("/img/bookPic/**")
                        .addResourceLocations("file:" + Constants.RESOURCE_WIN_PATH + "\\img\\bookPic\\");
            } else { // MAC、Linux系统
                registry.addResourceHandler("/img/bookPic/**")
                        .addResourceLocations("file:" + Constants.RESOURCE_MAC_PATH + "/img/bookPic/");
            }
        }
    }

//    添加歌单
    @ResponseBody
    @RequestMapping(value = "/book/add", method = RequestMethod.POST)
    public Object addBook(HttpServletRequest req){
        JSONObject jsonObject = new JSONObject();
        String title = req.getParameter("title").trim();
        String pic = req.getParameter("pic").trim();
        String introduction = req.getParameter("introduction").trim();
        String style = req.getParameter("style").trim();

        Book book = new Book();
        book.setTitle(title);
        book.setPic(pic);
        book.setIntroduction(introduction);
        book.setStyle(style);

        boolean res = bookService.addBook(book);
        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "添加成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "添加失败");
            return jsonObject;
        }
    }

//    返回所有歌单
    @RequestMapping(value = "/book", method = RequestMethod.GET)
    public Object allBook(){
        return bookService.allBook();
    }

//    返回指定标题对应的歌单
    @RequestMapping(value = "/book/title/detail", method = RequestMethod.GET)
    public Object bookOfTitle(HttpServletRequest req){
        String title = req.getParameter("title").trim();
        return bookService.bookOfTitle(title);
    }

//    返回标题包含文字的歌单
    @RequestMapping(value = "/book/likeTitle/detail", method = RequestMethod.GET)
    public Object bookOfLikeTitle(HttpServletRequest req){
        String title = req.getParameter("title").trim();
        return bookService.likeTitle('%'+ title + '%');
    }

//    删除歌单
    @RequestMapping(value = "/book/delete", method = RequestMethod.GET)
    public Object deleteBook(HttpServletRequest req){
        String id = req.getParameter("id");
        return bookService.deleteBook(Integer.parseInt(id));
    }

//    更新歌单信息
    @ResponseBody
    @RequestMapping(value = "/book/update", method = RequestMethod.POST)
    public Object updateBookMsg(HttpServletRequest req){
        JSONObject jsonObject = new JSONObject();
        String id = req.getParameter("id").trim();
        String title = req.getParameter("title").trim();
        String pic = req.getParameter("pic").trim();
        String introduction = req.getParameter("introduction").trim();
        String style = req.getParameter("style").trim();

        Book book = new Book();
        book.setId(Integer.parseInt(id));
        book.setTitle(title);
        book.setPic(pic);
        book.setIntroduction(introduction);
        book.setStyle(style);

        boolean res = bookService.updateBookMsg(book);
        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "修改成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "修改失败");
            return jsonObject;
        }
    }

//    更新歌单图片
    @ResponseBody
    @RequestMapping(value = "/book/img/update", method = RequestMethod.POST)
    public Object updateBookPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id")int id){
        JSONObject jsonObject = new JSONObject();

        if (avatorFile.isEmpty()) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "文件上传失败！");
            return jsonObject;
        }
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img" + System.getProperty("file.separator") + "bookPic" ;
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }

        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String storeAvatorPath = "/img/bookPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Book book = new Book();
            book.setId(id);
            book.setPic(storeAvatorPath);
            boolean res = bookService.updateBookImg(book);
            if (res){
                jsonObject.put("code", 1);
                jsonObject.put("avator", storeAvatorPath);
                jsonObject.put("msg", "上传成功");
                return jsonObject;
            }else {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败");
                return jsonObject;
            }
        }catch (IOException e){
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败" + e.getMessage());
            return jsonObject;
        }finally {
            return jsonObject;
        }
    }
}















